﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace iCreatePortal
{
    public partial class _Default : basePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                errorMsgLabel.Visible = false;
            }
            Session["ResID"] = -1;
            Session["PortalGroupID"] = -1;
            Session["ResName"] = "Guest";
            Session["RoleID"] = -1;
            userNameTextBox.Focus();
        }

        protected bool DoLogin(string userName, string password)
        {
            ConnectToDatabase();

            string loginString = "SELECT rp.ResID, rp.PortalPassword, rp.PortalGroupID, r.Name, r.RoleId FROM iCP_ResourcePortal rp INNER JOIN iCP_Resource r ON (rp.ResID=r.ID) WHERE UPPER(rp.LoginID)=@LoginID";
            SqlCommand cmdEmpl = new SqlCommand(loginString, connDB);
            SqlParameter paramLoginID = new SqlParameter("@LoginID", SqlDbType.NVarChar, 50);
            paramLoginID.Value = userName;
            cmdEmpl.Parameters.Add(paramLoginID);
            SqlDataAdapter daEmpl = new SqlDataAdapter(cmdEmpl);
            DataSet dsEmpl = new DataSet();
            try
            {
                daEmpl.Fill(dsEmpl, "EmpDetails");
            }
            finally
            {
                connDB.Close();
            }
            if (dsEmpl.Tables["EmpDetails"].Rows.Count != 0)
            {
                string userPwd = dsEmpl.Tables["EmpDetails"].Rows[0]["PortalPassword"].ToString();
                userPwd = Decrypt(userPwd);
                if (userPwd == password)
                {
                    Session["ResID"] = Convert.ToInt32(dsEmpl.Tables["EmpDetails"].Rows[0]["ResID"]);
                    int portalGroupId = Convert.ToInt32(dsEmpl.Tables["EmpDetails"].Rows[0]["PortalGroupID"]);
                    Session["PortalGroupID"] = portalGroupId;
                    Session["ResName"] = dsEmpl.Tables["EmpDetails"].Rows[0]["Name"].ToString();
                    Session["RoleID"] = Convert.ToInt32(dsEmpl.Tables["EmpDetails"].Rows[0]["RoleID"]);
                    switch (portalGroupId)
                    {
                        case -100:
                            Response.Redirect("~/DashboardA.aspx");
                            break;
                        case 1:
                            Response.Redirect("~/DashboardU.aspx");
                            break;
                        case 2:
                            Response.Redirect("~/DashboardP.aspx");
                            break;
                        case 3:
                            Response.Redirect("~/DashboardB.aspx");
                            break;
                        case 4:
                            Response.Redirect("~/DashboardR.aspx");
                            break;
                        case 5:
                            Response.Redirect("~/DashboardH.aspx");
                            break;
                        case 6:
                            Response.Redirect("~/DashboardM.aspx");
                            break;
                        case 7:
                            Response.Redirect("~/DashboardF.aspx");
                            break;
                        case 8:
                            Response.Redirect("~/DashboardI.aspx");
                            break;
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            errorMsgLabel.Visible = false;
            if (!DoLogin(userNameTextBox.Text, passwordTextBox.Text))
            {
                errorMsgLabel.Visible = true;
            }
        }
    }
}
